

import org.apache.hadoop.hive.ql.exec.UDF;

public class ConvertDateUdf extends UDF {
    public static String evaluate(String date){
        String newDate = "";
        String regularExpression1 = "[0-9]{4}年[0-9]{1,2}月.*";
        String regularExpression2 = "[0-9]{4}--[0-9]{1,2}.*||[0-9]{4}\\.[0-9]{1,2}--.*";
        String regularExpression3 = "[0-9]{4}\\.[0-9]{1,2}.*";
        String regularExpression4 = "[0-9]{4}-[0-9]{1,2}.*";
        String regularExpression5 = "[0-9]{6}.*";
        String regularExpression6 = "[0-9]{4}－.*";
        String regularExpression7 = "[0-9]{4} +[0-9]+";
        String regularExpression8 = "[0-9]{4}、.*";
        String regularExpression9 = "[0-9]{4}/.*";
        String regularExpression10 = "[0-9]{4},.*";
        String regularExpression11 = "[0-9]{4}，.*";
        if (date != null && !"".equals(date)) {
            String[] words;
            if (date.matches(regularExpression1)) {
                words = date.split("年");
                if (words[1].split("月")[0].length() == 1) {
                    newDate = words[0] + "0" + words[1].split("月")[0];
                } else {
                    newDate = words[0]  + words[1].split("月")[0];
                }

                return newDate;
            } else if (date.matches(regularExpression2)) {
                if (date.contains(".")) {
                    words = date.split("--");
                    String[] words2 = words[0].split("\\.");
                    if (words2[0].length() == 1) {
                        newDate = words2[0] + "0" + words2[1];
                    } else {
                        newDate = words2[0]  + words2[1];
                    }

                    return newDate;
                } else {
                    words = date.split("--");
                    if (words[1].length() == 1) {
                        newDate = words[0] + "0" + words[1];
                    } else {
                        newDate = words[0]  + words[1];
                    }

                    return newDate;
                }
            } else if (date.matches(regularExpression3)) {
                words = date.split("\\.");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1];
                }

                return newDate;
            } else if (date.matches(regularExpression4)) {
                words = date.split("-");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression5)) {
                newDate = date.substring(0, 4)  + date.substring(4, 6);
                return newDate;
            } else if (date.matches(regularExpression6)) {
                words = date.split("－");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression7)) {
                words = date.split(" +");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression8)) {
                words = date.split("、");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression9)) {
                words = date.split("/");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression10)) {
                words = date.split(",");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else if (date.matches(regularExpression11)) {
                words = date.split("，");
                if (words[1].length() == 1) {
                    newDate = words[0] + "0" + words[1];
                } else {
                    newDate = words[0]  + words[1].substring(0, 2);
                }

                return newDate;
            } else {
                return "false";
            }
        } else {
            return "false";
        }

    }
}
